#!usr/bin/env/ python
import sys

from showcase_class import Showcase
def _create_dataset ( start_point, lines, rows, tot_rows ):
        res = {}

        count = 0
        for v in rows:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1

        res [ 'rows' ] = tot_rows
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = lines

        return res

def add_conf ( liwe, id = 0 , name = '',  playtime = 0, w = 0, h = 0 ):
	"""
	:INPUT:
		- id:		SAFE_NUMBER	
		- name:		SAFE_DB_STRING
		- playtime:	SAFE_NUMBER
		- w:		SAFE_NUMBER
		- h:		SAFE_NUMBER
	:PERMS:
		- admin
	"""
	sc = Showcase ( liwe )
	res = sc.add_conf ( id = id , name = name, playtime = playtime, w = w, h = h )
	return { 'id_conf' : res }

def del_conf ( liwe, ids = '' ):
	"""
	:INPUT:
		- ids	SAFE_DB_STRING
	:PERMS:
		- admin
	"""
	sc = Showcase ( liwe )
	res = sc.del_conf ( ids = ids )
	return res



def list ( liwe, id_conf = 0, kind = '', ext = '', tag = '', _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
        """
        :INPUT:
                - kind:			SAFE_DB_STRING
                - id_conf:		SAFE_NUMBER
                - ext:			SAFE_DB_STRING
                - tag:			SAFE_DB_STRING
                - _X_LINES:		SAFE_NUMBER
                - _X_START_POINT:	SAFE_NUMBER
                - _X_PAGE:		SAFE_NUMBER
	:PERMS:
		- admin
        """
        use_dataset = 0
        quant = 1

        if _X_LINES != -1:
                quant = _X_LINES
                use_dataset = 1

        if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * quant


        sc = Showcase ( liwe )
        res = sc.list ( id_conf =  id_conf, kind = kind, ext = ext , tag = tag , start_point = start_point, do_count = use_dataset )

        rows = res [ 'rows' ]
        tot_rows = res [ 'tot_rows' ]

        if not use_dataset:
                if not rows: rows = []
                return { 'items': rows }

        return _create_dataset ( start_point, quant, rows, tot_rows )


def add ( liwe, id = 0, id_conf = 0, pos = 0, descr = '', lnk = '' ):
	"""
	:INPUT:
		- id:		SAFE_NUMBER
		- id_conf:	SAFE_NUMBER
		- pos:		SAFE_DB_INTEGER
		- descr:	SAFE_HTML
		- lnk:		SAFE_DB_STRING
	:PERMS:
		- admin
	"""


	sc = Showcase ( liwe )

	try:
		my_file = liwe.pyhp.uploads [ liwe.pyhp.uploads.keys () [ 0 ] ]
	except:
		my_file = None

	sys.stderr.write ( "----- showcase admin.py - uploads %s" % my_file )
        if not id and not my_file:
		# gestione errore
		liwe.err_code = 101
		liwe.err_descr = "Manca il file"
		return {}

	sc.add ( id = id, id_conf = id_conf, pos = pos, descr = descr, lnk = lnk, file_obj = my_file )


def delete ( liwe, ids = '' ):
	"""
	:INPUT:
		- ids	SAFE_DB_STRING
	:PERMS:
		- admin
	"""

	sys.stderr.write ( "----- showcase admin.py delete id:%s " % ids )
	sc = Showcase ( liwe )

	sc.delete ( ids )
	
def edit ( liwe, id = '' ):
	"""
	:INPUT:
		- id	SAFE_DB_STRING
	:PERMS:
		- admin
	"""
	sc = Showcase ( liwe )
	
	res = sc.get ( id ) 
	
	if not res : return {}
	return res 
	
